package com.example.studentmanager.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisAccessor;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.IOException;
import java.io.InputStream;


/**
 * 用于各类数据数据连接的基本配置
 */

public class BaseDao {
  public SqlSession  getSession(){
      // 根据 mybatis-config.xml 配置的信息得到 sqlSessionFactory
      String resource = "mybatis-config.xml";
      InputStream inputStream = null;
      try {
          inputStream = Resources.getResourceAsStream(resource);
      } catch (IOException e) {
          e.printStackTrace();
      }
      SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
      // 然后根据 sqlSessionFactory 得到 session
      SqlSession session = sqlSessionFactory.openSession();
      System.out.println(session.toString());
      return session;
  }


  public Jedis getJedis(){
      JedisPoolConfig jedisPoolConfig=new JedisPoolConfig();
      jedisPoolConfig.setMaxTotal(10);
      jedisPoolConfig.setMaxWaitMillis(5);
      jedisPoolConfig.setJmxEnabled(true);
      JedisPool jedisPool=new JedisPool(jedisPoolConfig,"localhost",6379,3000);
      //jedisPool.getResource();
      return jedisPool.getResource();
  }


}
